package org.jetlinks.pro.logging.excel;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.reactor.excel.CellDataType;
import org.hswebframework.reactor.excel.ExcelHeader;
import org.hswebframework.web.bean.FastBeanCopier;
import org.jetlinks.pro.logging.system.SerializableSystemLog;
import reactor.core.publisher.Flux;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author gyl
 * @time 2021/7/16 13:43
 */
@Getter
@Setter
@Slf4j
public class SystemLoggerExcelInfo {
    //线程名
    String threadName;
    //名称
    String name;
    //日志级别
    String level;
    //异常栈
    String exceptionStack;
    //服务名
    String server;
    //创建时间
    String createTime;
    //类名
    String className;
    //方法名
    String methodName;
    //行号
    int lineNumber;

    public SystemLoggerExcelInfo(SerializableSystemLog systemLog) {
        this.threadName = systemLog.getThreadName();
        this.name = systemLog.getName();
        this.level = systemLog.getLevel();
        this.exceptionStack = systemLog.getExceptionStack();
        this.server = systemLog.getContext().get("server");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.createTime = sdf.format(new Date(Long.parseLong(String.valueOf(systemLog.getCreateTime()))));
        this.className = systemLog.getClassName();
        this.methodName = systemLog.getMethodName();
        this.lineNumber = systemLog.getLineNumber();
    }


    public static List<ExcelHeader> getLoggerHeaderMapping() {
        return new ArrayList<>(Arrays.asList(
            new ExcelHeader("threadName", "线程名", CellDataType.STRING),
            new ExcelHeader("name", "名称", CellDataType.STRING),
            new ExcelHeader("level", "日志级别", CellDataType.STRING),
            new ExcelHeader("exceptionStack", "异常栈", CellDataType.STRING),
            new ExcelHeader("server", "服务名", CellDataType.STRING),
            new ExcelHeader("createTime", "创建时间", CellDataType.STRING),
            new ExcelHeader("className", "类名", CellDataType.STRING),
            new ExcelHeader("methodName", "方法名", CellDataType.STRING),
            new ExcelHeader("lineNumber", "行号", CellDataType.STRING)
        ));
    }

    public static Flux<SystemLoggerExcelInfo> getLoggerContentMapping(Flux<SerializableSystemLog> logs) {
        return logs.flatMap(log -> Flux.just(new SystemLoggerExcelInfo(log)))
                   .doOnError(err -> log.error(err.getMessage(), err));
    }

    public Map<String, Object> toMap(String format) {
        HashMap<String, Object> copy = FastBeanCopier.copy(this, new HashMap<>(11));
        if (format.equals("xlsx")) {
            for (String key : copy.keySet()) {
                String value = String.valueOf(copy.get(key));
                if (value.length() >= 32700) {
                    value = "该结果超出单元格限制，查看全部请使用csv格式导出.\n" + ((String) value).substring(0, 32700);
                    copy.put(key, value);
                }
            }
        }
        return copy;
    }

}


